<div class="box">
	<div class="box-header">
		<h3 class="box-title"><i class="fa fa-users"></i> <?=$this->lang->line(Common::isAdmin() ? "ADMINISTRATORS" : "USERS")?> <?=$this->lang->line("YOU_CAN_EDIT")?></h3>
	</div>
	<div class="box-body ">

		<? 
		//Common::pr($userList); exit; 
		?>

		<table id="userTable" class="display table table-bordered">
			<thead>
				<tr>
					<th class="text-right w1">#</th>
					<th><?=$this->lang->line("MAIL_ADDRESS")?></th>
					<th><?=$this->lang->line("TIME_OF_REGISTRATION")?></th>
					<th><?=$this->lang->line("TIME_OF_LAST_LOGIN")?></th>
					<th><?=$this->lang->line("CONFORMED")?></th>
					<th><?=$this->lang->line("SUPERADMIN_ADMIN")?></th>
					<th class="w1 ws"><?=$this->lang->line("OPTIONS")?></th>
				</tr>
			</thead>
			<tbody>
				<?php
				foreach($userList as $usr) {
					echo		"<tr>";
					echo			"<td class=\"vm w1 text-right\"></td>";
					echo			"<td class=\"vm\">".$usr["usrEmail"]."</td>";
					echo			"<td class=\"vm\">".Common::dateFromDatabase($usr["usrRegistered"])."</td>";
					echo			"<td class=\"vm\">".Common::dateFromDatabase($usr["usrLastupd"])."</td>";
					echo			"<td class=\"vm text-center\">".$this->lang->line($usr["usrVerify"] ? "NO" : "YES")."</td>";
					$superAdmin = $admin = FALSE;
					foreach($usr["grpName"] as $k => $v) {
						if($k == 1) $superAdmin = TRUE;
						elseif($k == 2) $admin = TRUE;
					}
					echo			"<td class=\"vm\">";
					echo				current($usr["grpName"]);

					echo				((($usr["u2gInsName"] and $usr["u2gInsId"])?("<br/><button class=\"btn btn-xs btn-default\" data-connect=\"true\" data-ins-id=\"".$usr["u2gInsId"]."\" data-ins-name=\"".$this->db->escape_str(strip_tags($usr["u2gInsName"]))."\" data-user=\"".$usr["usrId"]."\"><span class=\"glyphicon glyphicon-plus\"></span></button> ".$usr["u2gInsName"]):("")));

					echo				(($usr["u2gInsName"] and !$usr["u2gInsId"]) ? ("<br/><button class=\"btn btn-xs btn-default\" data-notexists=\"true\" data-ins-name=\"".$this->db->escape_str(strip_tags($usr["u2gInsNameRaw"]))."\" data-ins-address=\"".$this->db->escape_str($usr["u2gInsAddressRaw"])."\" data-ins-web=\"".$this->db->escape_str($usr["u2gInsWebRaw"])."\" data-ins-postal-code=\"".$this->db->escape_str($usr["u2gPostalCode"])."\" data-ins-city-id=\"".$this->db->escape_str($usr["u2gCityId"])."\"><span class=\"glyphicon glyphicon-plus\"></span></button> ".$usr["u2gInsName"]):(""));

					echo			"</td>";
					echo			"<td class=\"vm w1 ws\">";
					echo				"<button class=\"btn btn-xs btn-primary\" data-user=\"".$usr["usrId"]."\" data-entity=\"USER\" title=\"".$this->lang->line("EDIT")."\" ><span class=\"glyphicon glyphicon-edit\"></span></button>";
					echo				"&nbsp;<button class=\"btn btn-xs btn-primary\" ".($admin ? "" : "disabled")." data-privilege-user=\"".$usr["usrId"]."\" data-entity=\"USER\" data-email=\"".$usr["usrEmail"]."\" title=\"".$this->lang->line("PRIVILEGES")."\" ><span class=\"fa fa-unlock-alt\"></span></button>";
					echo				"&nbsp;<button class=\"btn btn-xs btn-primary\" data-user-email=\"".$usr["usrId"]."\" data-email=\"".$usr["usrEmail"]."\" title=\"".$this->lang->line("SENDING_E-MAIL")."\"><span class=\"glyphicon glyphicon-envelope\"></span></button>";
					echo				"&nbsp;<button class=\"btn btn-xs btn-danger\" data-user-delete=\"".$usr["usrId"]."\" data-entity=\"USER\" title=\"".$this->lang->line("DELETE")."\"><span class=\"glyphicon glyphicon-remove-circle\"></span></button>";
					echo			"</td>";
					echo		"</tr>";
				}
				?>
			</tbody>
		</table>

		<script>
			$(document).ready( function () {

				var userTable = $('#userTable').DataTable({
					"language": {
						"url": "<?=MEDIA?>admin_lte/DataTables-1.10.3/localization/en.lang"
					},
					"autoWidth": false,
					"columnDefs": [{
						"searchable": false,
						"orderable": false,
						"targets": 0
					}, {
						"searchable": false,
						"orderable": false,
						"targets": -1
					}],
					"order": [[ 1, 'asc' ]],
					stateSave: true
				});

				userTable.on('draw.dt', function () {
					userTable.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
						$(cell).addClass('text-right');
						cell.innerHTML = i+1 + '.';
					});
					eventRebind();
					$('#userTable').removeClass('hidden');
				});

				userTable.on( 'init.dt', function () {
					$('#userTable_length').append('<button class="btn btn-sm btn-success" style="margin-left: 20px;" data-user="-1" data-entity="USER"><span class="glyphicon glyphicon-plus"></span> <?=$this->lang->line("ADD_NEW")?></button>');
					eventRebind();
				});

			});

			function eventRebind() {
				$('button[data-user]').unbind('click').on('click', function(e) {
					e.preventDefault();
					$this = $(this);
					$.ajax({
						type: "POST",
						cache: false,
						data: { ENTITY: $this.data("entity"), ID: $this.data("user") },
						url: '<?=site_url("admin_lte/entity_edit")?>',
						beforeSend: function() {
							$('#studyEditorModal .modal-title').html(($this.data('user') == '-1' ? '<?=$this->db->escape_str($this->lang->line("ADD_NEW"))?>' : '<?=$this->lang->line("EDIT")?>') + ' ' + '<?=strtolower($this->lang->line("USER"))?>');
							$('#studyEditorModal .modal-body form').html('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');
							$('#studyEditorModal').modal('toggle');
						},
						success: function (result) {
							$('#studyEditorModal button.btn-success').data('url', '<?=site_url("admin_lte/entity_insert_update")?>');
							$('#studyEditorModal .modal-body form').html(result);
						},
						error: function (error) {}
					});
				});

				$('button[data-connect]').unbind('click').on('click', function() {
					$this = $(this);
					$('#studyConfirmModal .modal-title').html('<?=$this->lang->line("CONFIRMATION")?>');
					$('#studyConfirmModal .modal-body form').html('<?=$this->lang->line("ARE_YOU_SURE_YOU_WOULD_LIKE_TO_CONNECT_TO_USER_THE_INSTITUTE_BELOW")?><br/><strong>' + $this.data('insName') + '</strong>');
					$('#studyConfirmModal .modal-body form').append('<input type="hidden" name="usrId" value="' + $this.data('user') + '" /><input type="hidden" name="insId" value="' + $this.data('insId') + '" />');
					$('#studyConfirmModal button.btn-success').data('url', '<?=site_url("admin_lte/usr2grp_insert_update");?>');
					$('#studyConfirmModal').modal('toggle');
				});

				$('button[data-notexists]').unbind('click').on('click', function() {
					$this = $(this);

					$.ajax({
						type: "POST",
						cache: false,
						data: { ENTITY: 'INS', ID: -1, insName: $this.data('insName'), insAddress: $this.data('insAddress'), insWeb: $this.data('insWeb'), insPostalCode: $this.data('insPostalCode'), insCityId: $this.data('insCityId') },
						url: '<?=site_url("admin_lte/entity_edit")?>',
						beforeSend: function() {
							$('#studyEditorModal .modal-title').html('<?=$this->db->escape_str($this->lang->line("ADD_NEW"))?>' + '<?=strtolower($this->lang->line("SCHOOL"))?>');
							$('#studyEditorModal .modal-body form').html('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');
							$('#studyEditorModal').modal('toggle');
						},
						success: function (result) {
							$('#studyEditorModal button.btn-success').data('url', '<?=site_url("admin_lte/entity_insert_update")?>');
							$('#studyEditorModal .modal-body form').html(result);
						},
						error: function (error) {}
					});

				});

				$('button[data-user-email]').unbind('click').on('click', function(e) {
					e.preventDefault();
					$this = $(this);
					$.ajax({
						type: "POST",
						cache: false,
						data: { email: $this.data("email") },
						url: '<?=site_url("admin_lte/email_send_form")?>',
						beforeSend: function() {
							$('#studyEditorModal .modal-title').html('<?=$this->lang->line("SENDING_E-MAIL")?> - ' + $this.data('email'));
							$('#studyEditorModal .modal-body form').html('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');
							$('#studyEditorModal').modal('toggle');
						},
						success: function (result) {
							$('#studyEditorModal button.btn-success').data('url', '<?=site_url("admin_lte/email_send")?>');
							$('#studyEditorModal .modal-body form').html(result);
						},
						error: function (error) {}
					});
				});

				$('button[data-user-delete]').unbind('click').on('click', function() {
					$this = $(this);
					$('#studyDeleteModal .modal-title').html('<?=$this->lang->line("DELETE")?>');
					$('#studyDeleteModal .modal-body form').html('<?=$this->db->escape_str($this->lang->line("ARE_YOU_SURE_YOU_WOULD_LIKE_TO_DELETE_THE_ITEM"))?>');
					$('#studyDeleteModal .modal-body form').append('<input type="hidden" name="ENTITY" value="USER" /><input type="hidden" name="del" value="true" /><input type="hidden" name="ID" value="' + $this.data('userDelete') + '" />');
					$('#studyDeleteModal button.btn-danger').data('url', '<?=site_url("admin_lte/entity_insert_update");?>');
					$('#studyDeleteModal').modal('toggle');
				});

				$('button[data-privilege-user]').unbind('click').on('click', function(e) {
					e.preventDefault();
					$this = $(this);
					$.ajax({
						type: "POST",
						cache: false,
						data: { ID: $this.data("privilegeUser") },
						url: '<?=site_url("admin_lte/user_privileges")?>',
						beforeSend: function() {
							$('#studyDisplayModal .modal-title').html('<?=$this->lang->line("PRIVILEGES")?> - ' + $this.data('email'));
							$('#studyDisplayModal .modal-body').html('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');
							$('#studyDisplayModal').modal('toggle');
						},
						success: function (result) {
							$('#studyDisplayModal .modal-body').html(result);
						},
						error: function (error) {}
					});
				});

			}

		</script>

	</div>
</div>